#define _CRT_SECURE_NO_WARNINGS
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    ListNode* pcurA = headA;
    ListNode* pcurB = headB;
    int a = 0;
    int b = 0;
    while (pcurA)
    {
        a++;
        pcurA = pcurA->next;
    }
    while (pcurB)
    {
        b++;
        pcurB = pcurB->next;
    }
    ListNode* longhead = headA;
    ListNode* lesshead = headB;
    if (a < b)
    {
        longhead = headB;
        lesshead = headA;
    }
    int gap = abs(a - b);
    while (gap--)
    {
        longhead = longhead->next;
    }
    while (lesshead)
    {
        if (lesshead == longhead)
        {
            return longhead;
        }
        lesshead = lesshead->next;
        longhead = longhead->next;
    }
    return NULL;

}